package cn.wolfcode.business.service.impl;

import cn.wolfcode.business.domain.BusAppointment;
import cn.wolfcode.business.mapper.BusAppointmentMapper;
import cn.wolfcode.business.service.IBusAppointmentService;
import cn.wolfcode.business.util.DateUtil;
import cn.wolfcode.business.util.RegexUtils;
import cn.wolfcode.business.util.VehiclePlateNoUtil;
import cn.wolfcode.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * 养修信息预约Service业务层处理
 * 
 * @author wolfcode
 * @date 2025-09-18
 */
@Service
public class BusAppointmentServiceImpl implements IBusAppointmentService 
{
    @Autowired
    private BusAppointmentMapper busAppointmentMapper;

    /**
     * 查询养修信息预约
     * 
     * @param id 养修信息预约主键
     * @return 养修信息预约
     */
    @Override
    public BusAppointment selectBusAppointmentById(Long id)
    {
        return busAppointmentMapper.selectBusAppointmentById(id);
    }

    /**
     * 查询养修信息预约列表
     * 
     * @param busAppointment 养修信息预约
     * @return 养修信息预约
     */
    @Override
    public List<BusAppointment> selectBusAppointmentList(BusAppointment busAppointment)
    {
        return busAppointmentMapper.selectBusAppointmentList(busAppointment);
    }

    /**
     * 新增养修信息预约
     * 
     * @param busAppointment 养修信息预约
     * @return 结果
     */
    @Override
    public int insertBusAppointment(BusAppointment busAppointment)
    {
        if (busAppointment==null) {
            throw new RuntimeException("参数异常");
        }
        if (busAppointment.getCustomerName()==null
        || busAppointment.getLicensePlate()==null
        || busAppointment.getAppointmentTime()==null
        || busAppointment.getServiceType()==null
        || busAppointment.getCarSeries()==null
        || busAppointment.getCustomerPhone()==null) {
            throw new RuntimeException("参数异常");
        }
        if (!RegexUtils.isPhoneLegal(busAppointment.getCustomerPhone())) {
            throw new RuntimeException("手机号格式不正确");
        }
        if (VehiclePlateNoUtil.getVehiclePlateNo(busAppointment.getLicensePlate())==null) {
            throw new RuntimeException("车牌号格式不正确");
        }
        Date updateNow = DateUtil.changeDate(new Date(), Calendar.MINUTE, 1);
        if (!updateNow.before(busAppointment.getAppointmentTime())) {
            throw new RuntimeException("预约时间不能小于当前时间");
        }
//        BusAppointment busAppointment1 = new BusAppointment();
//        busAppointment1.setCustomerName(busAppointment.getCustomerName());
//        busAppointment1.setCustomerPhone(busAppointment.getCustomerPhone());
//        busAppointment1.setAppointmentTime(busAppointment.getAppointmentTime());
//        busAppointment1.setLicensePlate(busAppointment.getLicensePlate());
//        busAppointment1.setCarSeries(busAppointment.getCarSeries());
//        busAppointment1.setServiceType(busAppointment.getServiceType());
//        busAppointment1.setInfo(busAppointment.getInfo());

        busAppointment.setStatus(BusAppointment.STATUS_BOOKING);
        busAppointment.setCreateTime(DateUtils.getNowDate());
        return busAppointmentMapper.insertBusAppointment(busAppointment);
    }

    /**
     * 修改养修信息预约
     * 
     * @param busAppointment 养修信息预约
     * @return 结果
     */
    @Override
    public int updateBusAppointment(BusAppointment busAppointment)
    {
        if (busAppointment==null) {
            throw new RuntimeException("参数异常");
        }
        if (busAppointment.getCustomerName()==null
                || busAppointment.getLicensePlate()==null
                || busAppointment.getAppointmentTime()==null
                || busAppointment.getServiceType()==null
                || busAppointment.getCarSeries()==null
                || busAppointment.getCustomerPhone()==null) {
            throw new RuntimeException("参数异常");
        }
        if (!RegexUtils.isPhoneLegal(busAppointment.getCustomerPhone())) {
            throw new RuntimeException("手机号格式不正确");
        }
        if (VehiclePlateNoUtil.getVehiclePlateNo(busAppointment.getLicensePlate())==null) {
            throw new RuntimeException("车牌号格式不正确");
        }
        Date updateNow = DateUtil.changeDate(new Date(), Calendar.MINUTE, 1);
        if (!updateNow.before(busAppointment.getAppointmentTime())) {
            throw new RuntimeException("预约时间不能小于当前时间");
        }
        BusAppointment oldObj = this.selectBusAppointmentById(busAppointment.getId());
        if (oldObj==null) {
            throw new RuntimeException("数据不存在");
        }
        if (!busAppointment.getStatus().equals(oldObj.getStatus())){
            throw new RuntimeException("非法数据");
    }
        if (!BusAppointment.STATUS_BOOKING.equals(oldObj.getStatus())) {
            throw new RuntimeException("只有在预约中才可以进行编辑操作");
        }

        return busAppointmentMapper.updateBusAppointment(busAppointment);
    }

    /**
     * 批量删除养修信息预约
     * 
     * @param ids 需要删除的养修信息预约主键
     * @return 结果
     */
    @Override
    public int deleteBusAppointmentByIds(Long[] ids)
    {
        Long id = ids[0];
        if (id==null) {
            throw new RuntimeException("数据不存在");
        }
        BusAppointment busAppointment = this.selectBusAppointmentById(id);
        if(busAppointment==null){
            throw new RuntimeException("数据不存在");
        }
        if (!BusAppointment.STATUS_BOOKING.equals(busAppointment.getStatus())) {
            throw new RuntimeException("只有在预约中才可以进行删除操作");
        }
        return busAppointmentMapper.deleteBusAppointmentByIds(ids);
    }

    /**
     * 删除养修信息预约信息
     * 
     * @param id 养修信息预约主键
     * @return 结果
     */
    @Override
    public int deleteBusAppointmentById(Long id)
    {
        return busAppointmentMapper.deleteBusAppointmentById(id);
    }

    /**
     * 到店功能
     * @param id 养修信息预约主键
     * @return 是否到店成功
     */
    @Override
    public int arrival(Long id) {
        if (id == null) {
            throw new RuntimeException("数据不存在");
        }
        BusAppointment busAppointment = this.selectBusAppointmentById(id);
        if (busAppointment == null) {
            throw new RuntimeException("数据不存在");
        }
        if (!BusAppointment.STATUS_BOOKING.equals(busAppointment.getStatus())) {
            throw new RuntimeException("只有在预约中才可以进行到店操作");
        }
        return busAppointmentMapper.changeStatusAndArrivalTimeById(new Date(), BusAppointment.STATUS_ARRIVED, id);

    }

/**
 * 处理取消操作的方法
 * @param id 需要取消的项目的ID
 * @return 返回操作结果，0表示成功
 */
    @Override
    public int handleCancel(Long id) {
        if (id == null) {
            throw new RuntimeException("数据不存在");
        }
        BusAppointment busAppointment = this.selectBusAppointmentById(id);
        if (busAppointment == null) {
            throw new RuntimeException("数据不存在");
        }
        if (!BusAppointment.STATUS_BOOKING.equals(busAppointment.getStatus())) {
            throw new RuntimeException("只有在预约中才可以进行取消操作");
        }
        return busAppointmentMapper.changeStatusById(BusAppointment.STATUS_USER_CANCELLED, id);
    }

    @Override
    public void updateStatus(Integer statusPaid, Long appointmentId) {
        busAppointmentMapper.updateStatusById(statusPaid, appointmentId);
    }

}
